﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIFixedObj : MonoBehaviour
{
    private GameObject obj;    //跟随目标对象
    private RectTransform rect;
    private float scalerX;
    private float scalerY;

    public Image hp;
    public Text ememyname;

    bool isinit = false;

    bool isstarthp = false;

    public void Init(GameObject go, string name)
    {
        rect = (RectTransform)transform;
        CanvasScaler scaler = transform.root.GetComponent<CanvasScaler>();
        obj = go;
        ememyname.text = name;
        isinit = true;
    }

    void Update()
    {
        if (!isinit)
            return;
        Vector2 pos;
        Canvas canvas = transform.root.GetComponent<Canvas>();
        if (Camera.main == null) return;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
            Camera.main.WorldToScreenPoint(obj.transform.position), canvas.worldCamera, out pos);
        {
            rect.localPosition = pos;
        }

        if (isstarthp)
        {

        }
    }

    public void JXue(float harmvalue)
    {
        hp.fillAmount = harmvalue;
    }

    public void Createhp(int hp)
    {
        GameObject go = DataManager.GetInstance.Loadasset(ResourcesEnum.Prefab, "hptext");
        GameObject temp = Instantiate(go, this.transform, false);
        temp.GetComponent<ShowhpText>().Init(hp);
    }

}
